<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键词：">
          <el-input v-model="listPram.name" placeholder="请输入单位名称" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="handlerOpenEdit(0)" v-hasPermi="['admin:department:save']">添加单位</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" size="mini" :header-cell-style=" {fontWeight:'bold'}">
        <el-table-column
          prop="id"
          label="ID"
          width="50"
        />
        <el-table-column label="单位名称" prop="name" min-width="120"/>
        <el-table-column label="上级单位" prop="parentNames" min-width="230">
          <template slot-scope="scope" v-if="scope.row.parentNames && scope.row.parentNames != '--'">
            <el-tag size="small" type="info" v-for="(item, index) in scope.row.parentNames.split(',')" :key="index" class="mr5">{{ item }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="单位人数" prop="personCount" min-width="120"/>
        <el-table-column label="操作" min-width="130"  fixed="right">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" type="primary" size="mini" @click="handlerOpenEdit(1,scope.row)" v-hasPermi="['admin:department:info']">编辑</el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="handlerOpenDel(scope.row)" v-hasPermi="['admin:department:delete']">删除</el-button>
            <el-button icon="el-icon-user" type="success" size="mini" @click="" v-hasPermi="['admin:department:person:list']">人员列表</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="listPram.page"
        :page-sizes="constants.page.limit"
        :layout="constants.page.layout"
        :total="listData.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
      :visible.sync="editDialogConfig.visible"
      :title="editDialogConfig.isCreate === 0? '创建单位':'编辑单位'"
      destroy-on-close
      :close-on-click-modal="false"
      width="700px"
    >
      <edit
        v-if="editDialogConfig.visible"
        :is-create="editDialogConfig.isCreate"
        :edit-data="editDialogConfig.editData"
        :all-tree-list="listData.list"
        @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import * as systemAdminApi from '@/api/systemadmin.js'
import * as roleApi from '@/api/role.js'
import edit from './edit'
import { checkPermi } from "@/utils/permission"; // 权限判断函数
export default {
  // name: "index"
  components: { edit },
  data() {
    return {
      constants:this.$constants,
      listData: { list: [] },
      listPram: {
        page: 1,
        limit: this.$constants.page.limit[0]
      },
      editDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {}
      },
    }
  },
  mounted() {
    this.handleGetDepartmentList()
  },
  methods: {
    checkPermi,
    handleSearch() {
      this.listPram.page = 1
      this.handleGetDepartmentList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.handleGetDepartmentList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.handleGetDepartmentList()
    },
    handlerOpenDel(rowData) {
      this.$confirm('确认删除当前数据').then(() => {
        const _pram = { id: rowData.id}
        systemAdminApi.adminDel(_pram).then(data => {
          this.$message.success('删除数据成功')
          this.handleGetDepartmentList()
        })
      })
    },
    handleGetDepartmentList() {
      //systemAdminApi.adminList( this.listPram ).then(data => {
      //后期接口调用
      let data = {"page":1,"limit":100,"totalPage":1,"total":3,"list":[{"id":1,"name":"110部队","parentNames":"--","level":null,"status":true,"createTime":"2020-04-18 11:19:25","updateTime":"2022-01-07 11:08:40","personCount":11},{"id":2,"name":"装备管理处","parentNames":"110部队","level":null,"status":true,"createTime":"2020-04-18 11:19:25","updateTime":"2022-01-07 11:08:40","personCount":5},{"id":3,"name":"后勤保障处","parentNames":"110部队","level":null,"status":true,"createTime":"2020-04-18 11:19:25","updateTime":"2022-01-07 11:08:40","personCount":6}]};

        this.listData = data
        // this.handlerGetMenuList() //本身注释
      //})
    },
    handlerOpenEdit(isCreate, editDate) {
      this.editDialogConfig.editData = editDate
      this.editDialogConfig.isCreate = isCreate
      this.editDialogConfig.visible = true
    },
    /*handlerGetMenuList() { // 获取菜单全部数据后做menu翻译使用
      systemAdminApi.listCategroy({ page: 1, limit: 999, type: 5 }).then(data => {
        this.menuList = data.list
        this.listData.list.forEach(item => {
          const _muneText = []
          const menuids = item.rules.split(',')
          menuids.map(muid => {
            this.menuList.filter(menu => {
              if (menu.id == muid) {
                _muneText.push(menu.name)
              }
            })
          })
          item.rulesView = _muneText.join(',')
          this.$set(item, 'rulesViews', item.rulesView)
        })
      })
    },*/
    hideEditDialog() {
      this.editDialogConfig.visible = false
      this.handleGetAdminList()
    },
    resetQuery(){
      this.listPram.name = '';
      this.handleGetAdminList()
    }
  }
}
</script>

<style scoped>

</style>
